package net.starelement.gamelib;

import cn.nukkit.Server;
import cn.nukkit.event.EventHandler;
import cn.nukkit.event.inventory.InventoryPickupItemEvent;
import cn.nukkit.event.level.WeatherChangeEvent;
import cn.nukkit.event.player.PlayerDeathEvent;
import cn.nukkit.event.player.PlayerDropItemEvent;
import cn.nukkit.event.player.PlayerJoinEvent;
import cn.nukkit.scheduler.NukkitRunnable;

/**  StarElement Listener 事件监听器类
 * 这里只监听基础事件，玩法功能由游戏插件自身的监听器实现
 *
 * @author Ruok233
 * @since NukkitX API 1.0.9 | StarElement Project v1-alpaca
 *
 * update: 2019/12/07
 * 创建类
 */
public class Listener implements cn.nukkit.event.Listener {

    private ListenerConfig config;

    public Listener(ListenerConfig config) {
        this.config = config;
        if (config.lockTime) {
            Server.getInstance().getLevelByName("world").setTime(config.time);
            Server.getInstance().getLevelByName("world").stopTime();
        }
    }

    //死亡不掉落, 这里似乎没起作用
    @EventHandler
    public void playerDeath(PlayerDeathEvent event) {
        if (config.diedWithoutDrop) {
            event.setCancelled();
        }
    }

    //丢弃物品
    @EventHandler
    public void playerDropItem(PlayerDropItemEvent event) {
        if (config.noDiscard) {
            event.setCancelled();
        }
    }

    //锁定天气
    @EventHandler
    public void weatherChange(WeatherChangeEvent event) {
        if (config.weatherChange) {
            event.setCancelled();
        }
    }


    //捡拾物品
    @EventHandler
    public void inventoryPickupItem(InventoryPickupItemEvent event) {
        if (config.noPick) {
            event.setCancelled();
        }
    }

    //玩家加入
    @EventHandler
    public void playerJoin(PlayerJoinEvent event) {
        //异步处理将玩家传送至出生点
        if (config.joinToSpawn) {
            new NukkitRunnable() {
                @Override
                public void run() {
                    event.getPlayer().teleport(event.getPlayer().getLevel().getSafeSpawn());
                }
            }.runTask(GameLib.getInstance());
        }

        //修改游戏模式
        if (config.joinMode != 4) {
            event.getPlayer().setGamemode(config.joinMode);
        }
    }



}
